	var ws=new WebSocket("ws://10.222.29.187:8089/m-version3/ws/push");
	var icnum=$("#hiddenicnum").val();
	setTimeout("ws.send(icnum)","2000");
	ws.onmessage=function(event){
		if(event.data==="200"){
			var num=parseInt($("#unreadNum").html())+1;
			$("#unreadNum").html(num);
		}
		if(event.data==="300"){
			var num=parseInt($("#unreadNum").html())-1;
			$("#unreadNum").html(num);
		}
	
	
	}
	
    var strs = window.location.href.split("/");
    var urlPath = strs[0]+"/"+strs[1]+"/"+strs[2]+"/"+strs[3]+"/";
   //ajax请求
    var ajaxProduce = function(){
        var ajax = {};
        //requestConfig：一个对象，里面封装了url，data,contentType等属性
        //callback :表示ajax请求成功后后调的函数
        //otherParam 额外传给ajax回调函数的参数，根据自身需要传，可传可不传（一般传入待渲染的HTML元素）
        ajax.post = function(requestConfig,callback,myParam){
            var len = arguments.length;
             requestConfig.type="POST";
             $.ajax(requestConfig).done(function(data){
              if(len<3){
              callback(data);
             }else{
               callback(data,myParam);
             }
             });
           };
           
            ajax.get = function(requestConfig,callback,myParam){
             requestConfig.type="GET";
              var len = arguments.length;
             $.ajax(requestConfig).done(function(data){
             if(len<3){
              callback(data);
             }else{
               callback(data,myParam);
             }
             });
           };
      return ajax;
  }



//界面管理类
 function FoodListManager(template){
    
     var ajax = ajaxProduce();
     this.list ;
     
     this.initial = function(){
        //添加事件监听
          setAddFoodEvent();
          addBussiness();
        //初始化food列表 
      var requestConfig = {};
          requestConfig.url = urlPath+"foodslist";
          requestConfig.contentType = "application/json";
          ajax.get(requestConfig,loadFoodDiV,template);
         //初始化商家信息 
      var requestConfig1 = {};
          requestConfig1.url = urlPath+"getMerchant";
          requestConfig1.contentType = "application/json";    
          ajax.get(requestConfig1,loadMerchantsMsg);
              
     }
     //===========================================================
     //回调方法
     function loadFoodDiV(data,template){
        // alert("load...");
       this.list = data;
       senderFoodDiV(template);
       addEvent();
     }
     
     function addShow(data,tpl){
       this.list.push(data);
       senderFoodDiV(tpl);
       var fid=$("#fid").val("");
       var fname=$("#fname").val("");
       var fprice=$("#fprice").val("");
       var foodFile= $('#foodFile').val("");
        $("#addFoodModal").modal("hide");
         addEvent();
     }
     
     function deleteShow(data,tpl){
      this.list = this.list.filter(function(item){
                		return item.fid!=data.fid;
                	});
         senderFoodDiV(tpl);
         addEvent();
     }
     
     function puchShow(data,aa){
       alert("推送成功");
     }
     
     function updateShow(data,template){
          var fid=$("#ufid").val("");
          var fname=$("#ufname").val("");
          var fprice=$("#ufprice").val("");
          var foodFile= $('#ufoodFile').val("");
      this.list = data;
       senderFoodDiV(template);
       addEvent();
       $("#updateFoodModal").modal("hide");
     }
   
     function loadMerchantsMsg(data){
       $("#porName").text(data.mname);
       $("#porAddr").text(data.maddr);
       $("#portrait").attr("src",data.portrait);
       if(data.operatStatus==1){
         $("#showStatus").html("营业中...");
       }else{
    	 $("#showStatus").html("打烊中...");
       }
     }
     
     
     function showBussinessStatus(data,elem){
    	 if(data==1){
    		 elem.html("营业中...");
    	 }else if(data==2){
    		 elem.html("打烊中...");
    	 }else{
    		 alert("服务器有点忙...")
    	 }
    	 
    	 
     }
     
     //==================================================================
      //controler 方法
     function  updateFood(){
     var formData = new FormData();
          var fid=$("#ufid").val();
          var fname=$("#ufname").val();
          var ftype=$("#uftype").val();
          //alert(ftype);
          var fprice=$("#ufprice").val();
          var foodFile= $('#ufoodFile')[0].files[0];
          formData.append("fid",fid);  
         formData.append("fname",fname);
         formData.append("ftype",ftype);
         formData.append("fprice",fprice);
         formData.append("foodFile",foodFile);
        var requestConfig = {};
        requestConfig.processData= false;
        requestConfig.contentType= false;
        requestConfig.data = formData;
        requestConfig.url = urlPath+"updatefood";
         ajax.post(requestConfig,updateShow,template);
     }
     
     
     function  addFood(){
     var formData = new FormData();
          var fname=$("#fname").val();
          var ftype=$("#ftype").val();
          var fprice=$("#fprice").val();
          var foodFile= $('#foodFile')[0].files[0];  
         formData.append("fname",fname);
         formData.append("ftype",ftype);
         formData.append("fprice",fprice);
         formData.append("foodFile",foodFile);
        var requestConfig = {};
        requestConfig.processData= false;
        requestConfig.contentType= false;
        requestConfig.data = formData;
        requestConfig.url = urlPath+"addfood";
         ajax.post(requestConfig,addShow,template);
     }
     
     
     //============================================================
     //添加监听事件
     function addEvent(){
        $(".upFood").on("click",function(){
            $("#updateFoodModal").modal("show"); 
            var $this=$(this);
            //alert($this.data().fid);
            $("#ufid").val($this.data().fid);
            $("#ufname").val($this.data().fname);
            var ftype = $this.data().ftype-1;
            $("#uftype ").get(0).selectedIndex=ftype;
            $("#ufprice").val($this.data().fprice); 
       });
       
       
       
       $(".delete").on("click",function(){
           var id = $(this).attr("id");
           //var data = {"fid":id};
           var requestConfig={};
           requestConfig.url = urlPath+"deletefood?fid="+id;
           //requestConfig.data = JSON.stringify(data);
           //requestConfig.contentType = "application/json";
           ajax.get(requestConfig,deleteShow,template);
       });
       
       
        $(".addAd").on("click",function(){
           var id = $(this).attr("id");
           //alert(id);
           var data = {"fid":id};
           var requestConfig={};
           requestConfig.url = urlPath+"foodAd/addFoodAd";
           requestConfig.data = JSON.stringify(data);
           requestConfig.contentType = "application/json";
           ajax.post(requestConfig,puchShow,template);
       });
       
       
        $("#addDIV").on("click",function(){
            //alert();
            $("#addFoodModal").modal("show"); 
          });
   }
     function setAddFoodEvent(){
       $("#pushReply").on("click",function(){
         //$(this).unbind("click");
            addFood();
       });
       
        $("#submitUpdate").on("click",function(){
            updateFood();
       });
     }
     
      function addBussiness(){
    	$("#start").on("click",function(){
    		var requestConfig = {};
            requestConfig.url = urlPath+"dobusiness?operatStatus=1";
            ajax.get(requestConfig,showBussinessStatus,$("#showStatus"));
    		
    	});
    	
       $("#end").on("click",function(){
    	   var requestConfig = {};
           requestConfig.url = urlPath+"dobusiness?operatStatus=2";
           ajax.get(requestConfig,showBussinessStatus,$("#showStatus"));
    	});
    	  
    	  
      }
  }    
  
  
  

    //===============================================================================================================
     //视图渲染器
      function senderFoodDiV(FoodsDiV){
        var addDIV = $("#addDIV");
        FoodsDiV.html("");
        if(list){
         list.forEach(food=>{
           var fdiv = $("<div>").addClass("fd").addClass("col-md-3");
           var li1 = $("<li>");
           var img = $("<img>").addClass("foodsize").addClass("img-circle").addClass("imageTemplate").attr("src",food.fimg);
           li1.append(img);
           var li2 = $("<li>").addClass("fnameTemplate").css("margin-left","50px").text(food.fname);
           var li3 = $("<li>").addClass("fpriceTemplate").css("margin-left","50px").text(food.fprice);
           var li4 = $("<li>");
           var pushbt = $("<button>").addClass("order").addClass("btn").addClass("btn-default").addClass("orderTemplate").addClass("addAd").attr("id",food.fid).text("推荐");
           var updatebt = $("<button>").addClass("order").addClass("btn").addClass("btn-default").addClass("orderTemplate").addClass("upFood").data(food).text("修改");
           var deletebt = $("<button>").addClass("order").addClass("btn").addClass("btn-default").addClass("orderTemplate").addClass("delete").attr("id",food.fid).text("删除");
           li4.append(pushbt).append(updatebt).append(deletebt);
           fdiv.append(li1).append(li2).append(li3).append(li4);
           FoodsDiV.append(fdiv);
           
        });
        }
        FoodsDiV.append(addDIV);
      };
      

 $(function(){
  var manager = new FoodListManager($("#foodDiv"));
    manager.initial();
 });
 
 
 $(function(){
                setInterval(reload,2000);
                function reload(){
                	$.ajax({
                		type:"GET",
                		url:urlPath+"authority"
                	}).done(function(msg){
                		if(msg==1){
                			$("#black").modal("show");
                		}else{
                			$("#black").modal("hide");
                		}
                	});
                }
            });
    